var _ajax = (url, method, data, successFn = null, option = {}) => {
    // 超时，毫秒
    var timeout = option.timeout != undefined ? option.timeout : 10000
    var dataType = option.dataType != undefined ? option.dataType : 'json'
    var headers = option.headers != undefined ? option.headers : {}

    if (undefined == $) {
        var $ = layui.jquery,
            layer = layui.layer;
    }

    $.ajax({
        url: url,
        type: method,
        dataType: dataType,
        data: data,
        cache: false,
        timeout: timeout,
        headers: headers,
        success: function(json) {
            if (successFn) {
                successFn(json);
            }
        },
        error: function (json) {
            console.log(json)
            layer.msg(json.msg, {icon:5});
        }
    });
}

var get = (url, data = {}, successFn = null) => {
    if (successFn === null && typeof data == 'function') {
        _ajax(url, 'GET', {}, data)
    }else {
        _ajax(url, 'GET', data, successFn)
    }
}

var post = (url, data = {}, successFn) => {

    _ajax(url, 'POST', data, successFn)
}

export {get, post}